package com.example.Android_test.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.Android_test.R;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.specs.LanguageSpec;

import java.util.ArrayList;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 30.05.13
 * Time: 12:03
 * To change this template use File | Settings | File Templates.
 */
public class SelectLanguage extends Activity {
    public static final String TITLE = "SelectSetOfWords";

    private Button dsl_button_CurrentLang;
    private LinearLayout dsl_linearLayout_LangVariants;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.other_dialog_select_lang);
        this.setTitle(TITLE);

        dsl_button_CurrentLang = (Button) findViewById(R.id.dsl_buttonCurrentLang);
        dsl_linearLayout_LangVariants = (LinearLayout) findViewById(R.id.dsl_linearLayoutLangVariants);

        prepareLangButtons();
    }

    private void prepareLangButtons() {
        dsl_linearLayout_LangVariants.removeAllViews();

        LanguageSpec currentLang = SettingsHelper.INSTANCE.getCurrentLanguage();
        dsl_button_CurrentLang.setText(currentLang.getDescription());

        ArrayList<LanguageSpec> availableLanguages = SettingsHelper.INSTANCE.getAvailableLanguages();

        for (LanguageSpec lang: availableLanguages) {
            if (lang.equals(currentLang)) continue;

            Button button = new Button(this);
            button.setText(lang.getDescription());
            button.setTag(lang.getCode());
            button.setOnClickListener(new ButtonVariantLangListener());

            dsl_linearLayout_LangVariants.addView(button);
        }
    }

    /*private Context instance(){
        return this;
    }*/

    private class ButtonVariantLangListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Locale locale = new Locale((String) v.getTag());
            Locale.setDefault(locale);

            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

            /*Intent intent = new Intent(instance(), Main.class);
            startActivity(intent);*/

            // Clear selected set of word
            SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_WORD_SET_ID, "");


            /*Intent refresh = new Intent(Main.getAppContext(), Main.class);
            Main.getAppContext().
            startActivity(refresh);
            this.finish();*/


            setResult(RESULT_OK, null);
            finish();
        }
    }
}